/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projetCalendar.controllers.evenements;

import java.io.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.*;
import javax.servlet.http.*;
import projetCalendar.Model.Evenement;



public class NouveauEvenementController extends HttpServlet {
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProjetCalendarPU");
    private EntityManager manager = emf.createEntityManager();
    private EntityTransaction utx;
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        SimpleDateFormat sdf_param = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Evenement newEvent = request.getSession(true).getAttribute("evenement") != null ?
            (Evenement) request.getSession(true).getAttribute("evenement") : 
            new Evenement();
        String date_str = request.getParameter("date");
        if(date_str != null) {
            try {
                Date date = sdf_param.parse(date_str);
                newEvent.setDateDebut(date);
            } catch(Exception e) {}
        }
        request.setAttribute("evenement", newEvent);
        request.setAttribute("type_form", "creer");
        if(!isAjax(request)) {
            request.setAttribute("vuePartielle", "/WEB-INF/vues/evenements/form.html.jsp");
            getServletContext().getRequestDispatcher("/WEB-INF/vues/Layout.jsp").forward(request, response);
        } else {
            request.setAttribute("vuePartielle", "/WEB-INF/vues/evenements/form.js.jsp");
            getServletContext().getRequestDispatcher("/WEB-INF/vues/Layout.jsp").forward(request, response);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override public void destroy() {
        emf.close();
    }
    
    private boolean isAjax(HttpServletRequest request) {
        String xrequestedwith = request.getHeader("X-Requested-With");
        return (xrequestedwith != null);
    }

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
